HTML
<template>
    <div class="ranklist-container">
        <table class="ranklist-table">
            <thead>
                <tr>
                    <th class="rank-header">玩家</th>
                    <th class="rank-header">游戏积分</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="user in users" :key="user.id" class="rank-item">
                    <td class="user-info">
                        <div class="user-container">
                            <div class="rank-indicator">
                                <template v-if="user.globalRank <= 3">
                                    <el-icon v-if="user.globalRank === 1" class="crown gold"
                                        ><Trophy
                                    /></el-icon>
                                    <el-icon v-if="user.globalRank === 2" class="crown silver"
                                        ><Trophy
                                    /></el-icon>
                                    <el-icon v-if="user.globalRank === 3" class="crown bronze"
                                        ><Trophy
                                    /></el-icon>
                                </template>
                                <span v-else class="rank-number">{{ user.globalRank }}</span>
                            </div>
                            <img :src="user.photo" class="user-avatar" />
                            <div class="username-wrapper">
                                <span class="username">{{ user.username }}</span>
                                <span class="user-id">ID: {{ user.id }}</span>
                            </div>
                        </div>
                    </td>
                    <td class="user-score">
                        <div class="score-container">
                            <span class="score-value">{{ user.rating }}</span>
                            <div
                                class="score-bar"
                                :style="{ width: (user.rating / maxScore) * 100 + '%' }"
                            ></div>
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>

        <div class="pagination-container">
            <ul class="pagination">
                <li
                    class="page-item"
                    :class="{ disabled: current_page === 1 }"
                    @click="click_page(-2)"
                >
                    <a class="page-link">
                        <i class="arrow prev"></i>
                    </a>
                </li>
                <li
                    v-for="page in pages"
                    :key="page.pageId"
                    class="page-item"
                    :class="{ active: page.is_active }"
                    @click="click_page(page.pageId)"
                >
                    <a class="page-link">{{ page.pageId }}</a>
                </li>
                <li
                    class="page-item"
                    :class="{ disabled: current_page === totalPages }"
                    @click="click_page(-1)"
                >
                    <a class="page-link">
                        <i class="arrow next"></i>
                    </a>
                </li>
            </ul>
        </div>
    </div>
</template>
<script>
import { useUserStore } from '@/store/user'
import $ from 'jquery'
import { ref } from 'vue'
import { Trophy } from '@element-plus/icons-vue'

export default {
    components: {
        Trophy,
    },
    setup() {
        const store = useUserStore()

        let users = ref([])
        let total_users = 0
        let pages = ref([])
        let current_page = 1
        const totalPages = ref(0)
        const maxScore = ref(store.maxScore || 0)
        const click_page = (page) => {
            if (page === -2) page = current_page - 1
            if (page === -1) page = current_page + 1
            let max_pages = parseInt(Math.ceil(total_users / 4))
            if (page >= 1 && page <= max_pages) {
                pull_page(page) //加载一个新的分页
            }
        }
        const update_pages = () => {
            let max_pages = totalPages.value // 直接使用后端返回的总页数
            let new_pages = []
            for (let i = current_page - 2; i <= current_page + 2; i++) {
                if (i >= 1 && i <= max_pages) {
                    new_pages.push({
                        pageId: i,
                        is_active: i === current_page,
                    })
                }
            }
            pages.value = new_pages
        }

        const pull_page = (page) => {
            current_page = page
            $.ajax({
                url: 'https://www.xujiaojiaojiao.cn/api/ranklist/getlist/',
                data: {
                    page,
                    pageSize: 4,
                    _t: Date.now(), // 添加时间戳防止缓存
                },
                type: 'get',
                headers: {
                    Authorization: 'Bearer ' + store.token,
                },
                timeout: 5000, // 添加超时
                success(resp) {
                    if (!resp || !resp.users) {
                        console.error('排行榜数据格式错误:', resp)
                        return
                    }

                    users.value = resp.users
                    // 计算全局排名
                    const baseRank = (current_page - 1) * 4
                    users.value = users.value.map((user, index) => ({
                        ...user,
                        globalRank: baseRank + index + 1,
                    }))

                    total_users = resp.total
                    totalPages.value = resp.pages
                    update_pages()

                    // 进度条最大值取全局最高分
                    if (users.value.length > 0) {
                        maxScore.value = Math.max(...users.value.map((u) => u.rating))
                    }
                },
                error() {
                    console.error('排行榜数据获取失败')
                    // 添加错误处理和重试机制
                    setTimeout(() => pull_page(page), 3000)
                },
            })
        }
        // 确保在组件挂载时立即调用
        pull_page(current_page)

        return {
            users,
            total_users,
            pages,
            click_page,
            current_page,
            totalPages,
            maxScore,
            Trophy,
        }
    },
}
</script>

<style scoped>
/* 容器整体优化 */
.ranklist-container {
    max-width: 800px;
    margin: 2rem auto;
    padding: 2rem;
    background: #1a1a1a;
    border-radius: 20px;
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.4);
}

/* 表格布局优化 */
.ranklist-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0 12px;
    table-layout: fixed;
}

/* 表头设计 */
.rank-header {
    padding: 1.2rem 2rem;
    background: linear-gradient(145deg, #2d2d2d, #252525);
    color: #e0e0e0;
    font-size: 1.2rem;
    font-weight: 600;
    letter-spacing: 0.8px;
    &:first-child {
        border-radius: 12px 0 0 12px;
    }
    &:last-child {
        border-radius: 0 12px 12px 0;
    }
}

/* 行项设计 */
.rank-item {
    background: rgba(40, 40, 50, 0.8);
    border-radius: 12px;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    &:hover {
        transform: translateY(-2px);
        box-shadow: 0 6px 20px rgba(0, 150, 255, 0.15);
    }
}

/* 玩家列样式 */
.user-info {
    width: 60%;
    padding: 1rem 2rem;
    .user-container {
        display: flex;
        align-items: center;
        gap: 1.5rem;
        .rank-indicator {
            position: relative;
            width: 40px;
            height: 40px;
            display: flex;
            align-items: center;
            justify-content: center;

            .crown {
                font-size: 2.4rem;
                filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.3));
                animation: float 2s ease-in-out infinite;
                &.gold {
                    color: #ffd700;
                    text-shadow: 0 0 16px rgba(255, 215, 0, 0.6);
                }
                &.silver {
                    color: #c0c0c0;
                    text-shadow: 0 0 12px rgba(192, 192, 192, 0.5);
                }
                &.bronze {
                    color: #cd7f32;
                    text-shadow: 0 0 12px rgba(205, 127, 50, 0.5);
                }
            }
            .rank-number {
                font-size: 1.2rem;
                font-weight: 600;
                color: rgba(255, 255, 255, 0.8);
            }
        }
        .user-avatar {
            width: 48px;
            height: 48px;
            border-radius: 50%;
            border: 2px solid #404040;
            transition: all 0.3s ease;
        }
        .username-wrapper {
            flex: 1;
            .username {
                font-size: 1.1rem;
                font-weight: 600;
                color: #fff;
                display: block;
                margin-bottom: 4px;
            }
            .user-id {
                font-size: 0.9rem;
                color: rgba(255, 255, 255, 0.6);
            }
        }
    }
}

/* 积分列样式 */
.user-score {
    width: 40%;
    padding: 1rem 2rem;
    .score-container {
        position: relative;
        height: 40px;
        display: flex;
        align-items: center;
        .score-value {
            position: relative;
            z-index: 1;
            font-size: 1.2rem;
            font-weight: 600;
            color: #00ff88;
            text-shadow: 0 0 8px rgba(0, 255, 136, 0.3);
        }
        .score-bar {
            position: absolute;
            left: 0;
            top: 0;
            height: 100%;
            background: linear-gradient(90deg, rgba(0, 255, 136, 0.15), rgba(0, 255, 136, 0.25));
            border-radius: 0 6px 6px 0;
            transition: width 0.6s ease;
        }
    }
}

/* 全新分页设计 */
.pagination-container {
    margin-top: 2rem;
    background: rgba(30, 30, 40, 0.8);
    border-radius: 16px;
    padding: 1rem;
    backdrop-filter: blur(8px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}

.pagination {
    display: flex;
    gap: 8px;
    justify-content: center;
    .page-item {
        width: 40px;
        height: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 10px;
        background: rgba(106, 17, 255, 0.1);
        transition: all 0.3s ease;
        cursor: pointer;

        &:hover:not(.active, .disabled) {
            background: rgba(106, 17, 255, 0.3);
            transform: translateY(-2px);
        }

        &.active {
            background: linear-gradient(45deg, #6a11ff, #4b7dff);
            box-shadow: 0 4px 16px rgba(106, 17, 255, 0.3);
            .page-link {
                color: #fff;
            }
        }

        &.disabled {
            opacity: 0.4;
            cursor: not-allowed;
        }

        .page-link {
            color: rgba(255, 255, 255, 0.8);
            font-weight: 500;
            display: block;
            width: 100%;
            text-align: center;
        }

        .arrow {
            border-color: rgba(255, 255, 255, 0.8);
        }
    }
}

/* 动画效果 */
@keyframes float {
    0%,
    100% {
        transform: translateY(0);
    }
    50% {
        transform: translateY(-4px);
    }
}

/* 响应式设计 */
@media (max-width: 768px) {
    .ranklist-container {
        margin: 1rem;
        padding: 1rem;
    }

    .user-info,
    .user-score {
        padding: 1rem;
    }

    .user-avatar {
        width: 40px;
        height: 40px;
    }

    .crown {
        font-size: 2rem;
    }
}
</style>
